package com.xgj.ph.servlet;

import com.alibaba.fastjson.JSON;
import com.xgj.ph.dao.GoodsDao;
import com.xgj.ph.dao.impl.goodsDaoImpl;
import com.xgj.ph.entity.Goods;
import com.xgj.ph.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/goods")
public class GoodsServlet extends HttpServlet {
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String action = req.getParameter("action");
    GoodsDao goodsDao = new goodsDaoImpl();
    switch (action){
      case "selectAll":
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
        String startTime = req.getParameter("startTime");
        String endTime = req.getParameter("endTime");
        String goodsName = req.getParameter("goodsName");
        String goodsStatus = req.getParameter("goodsStatus");
        String goodsShopName = req.getParameter("goodsShopName");
        //  3.2 封装到map集合
        Map<String, String> map = new HashMap<String, String>();
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        map.put("goodsStatus", goodsStatus);
        map.put("goodsName",goodsName);
        map.put("goodsShopName",goodsShopName);
        List<Goods> list = goodsDao.selectAll(map);
        int count = goodsDao.selectCount(map);
        Map resultMap = new HashMap();
        resultMap.put("list",list);
        resultMap.put("total",count);
        Result result = Result.success(resultMap);
        resp.getWriter().write(JSON.toJSONString(result));
        break;
      case "update":
        int addOrUpdateFlag = Integer.parseInt(req.getParameter("addOrUpdateFlag"));
        Result result1 = null;
        String id = req.getParameter("goodsId");
        int goodsId = 0;
        if (id != null && !"".equals(id)){
          goodsId = Integer.parseInt(id);
        }
        String goodsName1 = req.getParameter("goodsName");
        String presentPrice = req.getParameter("goodsPresentPrice");
        int goodsPresentPrice = 0;
        if (presentPrice != null && !"".equals(presentPrice)){
          goodsPresentPrice = Integer.parseInt(presentPrice);
        }
        String salesVolume = req.getParameter("goodsSalesVolume");
        int goodsSalesVolume = 0;
        if (salesVolume != null && !"".equals(salesVolume)){
          goodsSalesVolume = Integer.parseInt(salesVolume);
        }
        String goodsImage = req.getParameter("goodsImage");
        String status = req.getParameter("goodsStatus");
        int goodsStatus1 = 0;
        if (status != null && !"".equals(status)){
          goodsStatus1 = Integer.parseInt(status);
        }
        String id1 = req.getParameter("shopId");
        int shopId = 0;
        if (id1 != null && !"".equals(id1)){
          shopId = Integer.parseInt(id1);
        }
        String goodsShopName1 = req.getParameter("goodsShopName");
        String goodsCartCount = req.getParameter("goodsCartCount");
        String categoryId = req.getParameter("shopCategoryId");
        int shopCategoryId = 0;
        if (categoryId != null && !"".equals(categoryId)){
          shopCategoryId = Integer.parseInt(categoryId);
        }
        String goodsUnit = req.getParameter("goodsUnit");
        Goods goods = new Goods(
                goodsId,
                goodsName1,
                goodsPresentPrice,
                goodsSalesVolume,
                goodsImage,
                new Date(),
                goodsStatus1,
                shopId,
                goodsShopName1,
                goodsCartCount,
                shopCategoryId,
                goodsUnit
        );
        if (addOrUpdateFlag == 1){
          int insert = goodsDao.insert(goods);
          if (insert == 1){
            result1 = Result.success("添加成功");
          }else {
            result1 = Result.success("添加失败");
          }
        }else {
          int update = goodsDao.update(goods);
          if (update == 1){
           result1 = Result.success("修改成功");
          }else {
            result1 = Result.success("修改失败");
          }
        }
        resp.getWriter().write(JSON.toJSONString(result1));
        break;
      case "delete":
        String id2 = req.getParameter("goodsId");
        Result result2 = null;
        int goodsId2 = 0;
        if (id2 != null && !"".equals(id2)){
          goodsId2 = Integer.parseInt(id2);
        }
        int delete = goodsDao.delete(goodsId2);
        if (delete == 1){
          result2 = Result.success("删除成功");
        }else {
          result2 = Result.success("删除失败");
        }
        resp.getWriter().write(JSON.toJSONString(result2));
        break;
      default:break;
    }
  }
  
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    doGet(req, resp);
  }
}
